import apiClient from './axios'
import type { User } from '@/types/server'

interface LoginRequest {
  username: string
  password: string
}

interface LoginResponse {
  access: string
  refresh: string
  user?: User
}

interface RegisterRequest {
  username: string
  email: string
  password: string
}

interface RegisterResponse {
  user_id: number
  username: string
  email: string
  token: string
}

export const login = async (data: LoginRequest): Promise<LoginResponse> => {
  const response = await apiClient.post('/api/token/', data)
  return response.data
}

export const refreshToken = async (refresh: string): Promise<{ access: string }> => {
  const response = await apiClient.post('/api/token/refresh/', { refresh })
  return response.data
}

export const register = async (data: RegisterRequest): Promise<RegisterResponse> => {
  const response = await apiClient.post('/auth/register/', data)
  return response.data
}

export const getCurrentUser = async (): Promise<User> => {
  const response = await apiClient.get('/auth/me/')
  return response.data
}

export const logout = (): void => {
  localStorage.removeItem('auth_token')
  localStorage.removeItem('refresh_token')
  localStorage.removeItem('user_info')
} 